<?php

namespace App;

use App\Observers\UserObserver;
use App\Scopes\AgeScope;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;


class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];


    protected static $userArr = [];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

//    protected $appends = ['is_admin'];

    public $timestamps = true;


    //


    public function setUser($v)
    {
        self::$userArr[$v->id] = get_object_vars($v);
    }

    public function getuser()
    {

        return self::$userArr;

    }

    /**
     * 模型的「启动」方法
     *
     * @return void
     */
    protected static function boot()
    {
        parent::boot();

//        注册全局筛选
//        static::addGlobalScope(new AgeScope);

        //观察者
//        static::observe(new UserObserver);
    }

    /**
     * 只查询受欢迎的用户的作用域
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopePopular($query)
    {
        return $query->where('email', '=', 'john@example.com');
    }


    //返回is_admin属性
    public function getIsAdminAttribute()
    {
//        return $this->attributes['supplier_id'] === 2;
    }

    /**
     * 只查询 active 用户的作用域
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeActive($query)
    {
        return $query->where('name', 'hdd');
    }

    /**
     * 将查询作用域限制为仅包含给定类型的用户
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @param mixed $type
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeOfType($query, $type)
    {
        return $query->where('password', $type);
    }

    /**
     * 模型的事件映射
     *
     * @var array
     */
    protected $dispatchesEvents = [
        'saved' => UserSaved::class,
        'deleted' => UserDeleted::class,
    ];


    public function identity_card()
    {
        return $this->hasOne(IdentityCard::class, 'user_id', 'id');
    }

    /**
     * 用户拥有的角色
     */
    public function roles()
    {
        return $this->hasMany('App\AdminRole', 'admin_id', 'id');
    }

    public function identity_cards()
    {
        return $this->hasMany(IdentityCard::class, 'user_id', 'id');
    }


}
